/*
 * 创建日期 2005-10-8
 *
 * 更改所生成文件模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
package com.vanda.report.dp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.vanda.report.ReportException;
import com.vanda.report.ReportInnerData;
import com.vanda.report.Validation;
import com.vanda.report.element.BeanElement;

/*
	<function property="reportList" maxgroup="f1,f2,f3">
		<group field="region,country">
			<sum field="amount1,amount2" />
			<clean field="f1,f2,f3" />
			<write field="field1" message="Total" />
		</group>
	</function>
*/

/**
 * 函数处理开始标签 <br>
 * <p>
 * 
 * @author eagle <br>
 * @version Version 1.00 <br>
 */
public class FunctionElement extends BeanElement {

	private String maxgroup = null;
	
	public void validator() throws ReportException{
		Validation.checkRequired("FunctionElement","property",super.getProperty());
	}
	
	/**
	 * 标签执行方法，递归执行
	 * 临时变量说明:
	 * temp1:原始数据list
	 * temp2:当前行数
	 * temp3:Object[]数组,0-List(保存顺序号) ,1-HashMap(key-顺序号,value-需要新插入的值)
	 * @param obj
	 */
	public void execute(Object obj) throws ReportException{
		ReportInnerData reportData = (ReportInnerData)obj;
		String property = super.getProperty();
		
		try{
			reportData.setTemp1(null);
			reportData.setTemp2(null);
			reportData.setTemp3(null);
			
			Object[] objs = new Object[2];
			//保存顺序
			List lstIndex = new ArrayList();
			//HashMap用于查询
			HashMap hmIndex = new HashMap();
			objs[0] = lstIndex;
			objs[1] = hmIndex;
			
			reportData.setTemp3(objs);
			
			List objList = (List)super.getListObject(reportData, property);
			reportData.setTemp1(objList);
			
			for(int i=objList.size()-1;i>=0;i--){
				Object temp = objList.get(i);
				reportData.setContextData(temp);
				reportData.setTemp2(new Integer(i));
				super.execute(obj);
			}
			
			for(int i=0;i<lstIndex.size();i++){
				Integer key = (Integer)lstIndex.get(i);
				int index = key.intValue();
				
				List lstNew = (List)hmIndex.get(key);
				for(int j=lstNew.size()-1;j>=0;j--){
					objList.add(index,lstNew.get(j));
				}
			}
			

			reportData.setTemp1(null);
			reportData.setTemp2(null);
			reportData.setTemp3(null);
		}catch(ReportException e){
			throw e;
		}catch(Exception e){
			throw new ReportException(e);
		}

	}
	
	/**
	 * 克隆部分数据
	 * @param objs
	 * @param start
	 * @param end
	 * @return
	 */
	/*private List cloneData(Object objs,int start,int end) throws Exception{
		List lstClone = new ArrayList();
		if (objs instanceof List) {
			List listObjs = (List) objs;

			for (int i = start; i < end; i++) {
				Object obj = listObjs.get(i);
				Object objClone = ReportUtils.clone(obj);
				//hmClone.put(objClone,obj);
				lstClone.add(objClone);
			}
		} else {
			Object[] objArray = (Object[]) objs;
			for (int i = start; i < end; i++) {
				Object obj = objArray[i];
				Object objClone = ReportUtils.clone(obj);
				//hmClone.put(objClone,obj);
				lstClone.add(objClone);
			}
		}
		return lstClone;
	}*/

	/**
	 * @return
	 */
	public String getMaxgroup() {
		return maxgroup;
	}

	/**
	 * @param string
	 */
	public void setMaxgroup(String string) {
		maxgroup = string;
	}

}
